const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa2-cors");
app.use(cors())
app.use(parser());


//注册账号
let arr = [
  {
    username: "admin",
    password: "123456",
  },
]
//数据列表
let studentList = [
  {
    id: "1",
    img: "http://img.heilanhome.com/hfntbj200014/1615858062721.jpg",
    name: "海澜优选2021春夏新品情侣T恤play系列圆领潮趣印花男士T恤",
    cell: "￥128.00",
    type: "服装",
    num: "300"
  },
  {
    id: "2",
    img: "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg",
    name: "华为 HUAWEI P20",
    cell: "￥3788",
    type: "电子产品",
    num: "1200"
  },
  {
    id: "3",
    img: "http://img.heilanhome.com/720000459/1541570924630.jpg",
    name: "透明茶具套装",
    cell: "￥168",
    type: "家庭用品",
    num: "120"
  },
  {
    id: "4",
    img: "http://img.heilanhome.com/hfntbj200012/1615858126533.jpg",
    name: "海澜优选2021春夏新品情侣甜蜜百搭爱心系列棉质男款短袖T恤",
    cell: "￥168",
    type: "服装",
    num: "3000"
  },
  {
    id: "5",
    img: "https://cdn.leishen.cn/upload/goods/2021011208150601962_BIG.jpg",
    name: "雷神911MT黑武士豪华版",
    cell: "￥7499",
    type: "电子产品",
    num: "200"
  },
  {
    id: "6",
    img: "https://cdn.leishen.cn/upload/goods/2019122010502441643_BIG.jpg",
    name: "雷神M201幻彩RGB电竞游戏鼠标",
    cell: "￥109",
    type: "电子产品",
    num: "30"
  },
  {
    id: "7",
    img: "https://cdns.lining.com/postsystem/docroot/images/goods/202103/632981/display_632981_5.jpg",
    name: "李宁寻才问料 军布包裹男子宽松短袖文化衫",
    cell: "￥199",
    type: "服装",
    num: "3000"
  },
  {
    id: "8",
    img: "https://cdns.lining.com/postsystem/docroot/images/goods/202102/630493/display_630493_1.jpg",
    name: "李宁X成龙功夫系列男子宽松短袖文化衫",
    cell: "￥499",
    type: "服装",
    num: "700"
  },
  {
    id: "9",
    img: "http://img.heilanhome.com/740000426/1578706629298.jpg",
    name: "护颈椎助睡眠单人家用美容乳胶枕枕头",
    cell: "￥208",
    type: "家庭用品",
    num: "600"
  },
  {
    id: "10",
    img: "http://img.heilanhome.com/740000314/1546050064794.jpg",
    name: "抱枕-圣诞鹿",
    cell: "￥78",
    type: "家庭用品",
    num: "8000"
  },
  {
    id: "11",
    img: "https://cdn.leishen.cn/upload/goods/2019122010543689665_BIG.jpg",
    name: "雷神黑武士电竞显示器27英寸",
    cell: "￥1199",
    type: "电子产品",
    num: "120"
  },
  {
    id: "12",
    img: "https://cdn.leishen.cn/upload/common/2020122604585409524_MID.jpg",
    name: "雷神Q27HF广视角电竞显示器",
    cell: "￥2199",
    type: "电子产品",
    num: "3000"
  },
  {
    id: "13",
    img: "https://cdns.lining.com/postsystem/docroot/images/goods/201907/491329/display_491329_1.jpg",
    name: "运动时尚系列男子针织外套",
    cell: "￥278",
    type: "服装",
    num: "3000"
  },
  {
    id: "14",
    img: "http://img.heilanhome.com/720000552/1584494977671.jpg",
    name: "创意新骨瓷可爱情侣圣诞马克杯水杯咖啡杯450ml",
    cell: "￥20",
    type: "家庭用品",
    num: "5000"
  },
  {
    id: "15",
    img: "https://cdn.leishen.cn/upload/goods/2019112601391587756_BIG.png",
    name: "雷神K75B蓝血人104键机械键盘-黑轴",
    cell: "￥329",
    type: "电子产品",
    num: "800"
  },
  {
    id: "16",
    img: "https://cdns.lining.com/postsystem/docroot/images/goods/202101/625009/display_625009_5.jpg",
    name: "运动时尚系列男女同款宽松套头连帽卫衣",
    cell: "￥238",
    type: "服装",
    num: "200"
  },
  {
    id: "17",
    img: "http://img.heilanhome.com/780000111/1541576445772.jpg",
    name: "萌兔牵引背包",
    cell: "￥48",
    type: "家庭用品",
    num: "1000"
  },
  {
    id: "18",
    img: "https://cdn.leishen.cn/upload/goods/2019122011105999723_MID.jpg",
    name: "雷神战斧G50游戏手柄",
    cell: "￥209",
    type: "电子产品",
    num: "200"
  },
  {
    id: "19",
    img: "https://cdn.leishen.cn/upload/goods/2019122010591464281_BIG.jpg",
    name: "雷神Force T9Ti 游戏台式主机",
    cell: "￥19999",
    type: "电子产品",
    num: "50"
  },
  {
    id: "20",
    img: "https://cdns.lining.com/postsystem/docroot/images/goods/201505/218065/display_218065_1.jpg",
    name: "韦德之道3“柠檬滴”男子篮球专业比赛鞋",
    cell: "￥899",
    type: "服装",
    num: "800"
  },
]

//修改密码
router.put("/alter/:index", async ctx => {
  let ind = ctx.params.index;
  let setfruit = ctx.request.body.modfruit;
  console.log(setfruit)
  arr.splice(ind, 1, setfruit);
  ctx.body = true;
})

//登录判断
router.post("/login", async ctx => {
  let login = ctx.request.body.login;
  let code = "";
  let isLogin = arr.find(item => {
    return login.username === item.username && login.password === item.password
  })
  if (isLogin) {
    code = 200
  } else {
    code = 400
  }
  ctx.body = { code }
})

//获取注册信息
router.get("/sign", async ctx => {
  ctx.body = arr
})
// 注册
router.post("/sign", async ctx => {
  let sign = ctx.request.body.sign;
  let code = "";
  let isCreate = arr.find(e => {
    return sign.username === e.username
  })
  if (!isCreate) {
    code = 200
    arr.push(sign)
    console.log(sign)
    console.log(arr)
  } else {
    code = 400
  }
  ctx.body = { code }
})
//获取数据
router.get("/student", async ctx => {
  ctx.body = studentList
})
//添加数据
router.post("/student", async ctx => {
  let student = ctx.request.body.student;
  studentList.push(student);
  // ctx.body = studentList
  ctx.body = true
})
//删除数据
router.delete("/student/:id", async ctx => {
  let id = ctx.params.id;
  console.log(id)
  studentList.splice(id, 1);
  ctx.body = true
})
//修改数据
router.put("/student/:index", async ctx => {
  let ind = ctx.params.index;
  let setUsr = ctx.request.body.student;
  console.log(setUsr);
  studentList.splice(ind, 1, setUsr);
  ctx.body = true;
})
app.use(router.routes());
app.listen(3000, () => {
  console.log("3000端口")
})